package com.my.algorithm;

import com.my.algorithm.CarbonDeclareUtil;
import com.my.algorithm.entity.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class CarbonUtil {
      /**
              * 核查
     * 自动计算  总体情况 数据
     * @param check
     * @param score
     * @return*/

    public static  CarbonDeclareScore computeCheck(CarbonCheck check, CarbonDeclareScore score){
        //        计算
//        市场履约率
        if(check.getFiveYearFulfill()!=null&&check.getFiveYearFulfill().equals(1))
            score.setFulfillRate(new BigDecimal(100));
        else
            score.setFulfillRate(new BigDecimal(0));
//        碳资产情况
        if(check.getCcerNum().compareTo(BigDecimal.ZERO)>0
                || check.getVcsNum().compareTo(BigDecimal.ZERO)>0
                || check.getGreenElectricityNum().compareTo(BigDecimal.ZERO)>0)
            score.setPropertyAuto(new BigDecimal(100));
        else  score.setPropertyAuto(new BigDecimal(0));
//    绿色供应链
        if(check.getGreenSupplyLevel()==null)
            score.setGreenISCAuth(new BigDecimal(0));
        else if(check.getGreenSupplyLevel().equals(14))
            score.setGreenISCAuth(new BigDecimal(100));
        else if(check.getGreenSupplyLevel().equals(15))
            score.setGreenISCAuth(new BigDecimal(60));
        else  score.setGreenISCAuth(new BigDecimal(0));
//      绿色供应链平台
        if(check.getGreenTrace()==null||check.getGreenTrace().equals(0))
            score.setGreenISCPlate(new BigDecimal(0));
        else  score.setGreenISCPlate(new BigDecimal(100));
//        绿色工厂认证
        if(check.getGreenFactoryLevel()==null)
            score.setExpandAuth(new BigDecimal(0));
        else if(check.getGreenFactoryLevel().equals(14))
            score.setExpandAuth(new BigDecimal(100));
        else if(check.getGreenFactoryLevel().equals(15))
            score.setExpandAuth(new BigDecimal(60));
        else  score.setExpandAuth(new BigDecimal(0));
//        绿色产品认证
        int num=0;
        if(check.getGreenProduct()!=null)
            num=check.getGreenProduct().size();
        num=num*10;
        score.setExpandNum(new BigDecimal(num));
        if(score.getProductEnergy()==null)
            score.setProductEnergy(new BigDecimal(0));
        if(score.getEnergyStruct()==null)
            score.setEnergyStruct(new BigDecimal(0));
        if(score.getCarbonIntensity()==null)
            score.setCarbonIntensity(new BigDecimal(0));
        if(score.getCarbonStruct()==null)
            score.setCarbonStruct(new BigDecimal(0));
        if(score.getGreenEnergyAve()==null)
            score.setGreenEnergyAve(new BigDecimal(0));
        if(score.getGreenEnergyTwenty()==null)
            score.setGreenEnergyTwenty(new BigDecimal(0));
        if(score.getPropertyScore()==null)
            score.setPropertyScore(new BigDecimal(0));

        return score;
    }
    /**
            * 核查
     * 计算单位排放及总排放
     * @param products
     * @param scores
     * @param carbon
     * @return*/

    public static CompanyCarbon computeCheck(  List<CarbonCheckProduct> products, List<CarbonCheckProductScore> scores,CompanyCarbon carbon){
        if(products==null)
            return carbon;
        BigDecimal energy=new BigDecimal(0),co=new BigDecimal(0),danenergy=new BigDecimal(0),danco=new BigDecimal(0);

        if(products!=null&&products.size()>0){
            for(CarbonCheckProduct product:products){
                if(product.getId()!=null){
                    CarbonCheckProductScore score= scores.stream().filter(p->p.getProductId().equals(product.getId())).findFirst().get();
                    if(score!=null){
                        if(product.getNum()==null)
                            product.setNum(new BigDecimal(0));
                        energy=energy.add(score.getEnergy());
                        co=co.add(score.getExhaust());
                        danenergy=danenergy.add(energy.divide(product.getNum(),4,BigDecimal.ROUND_HALF_UP));
                        danco=danco.add(co.divide(product.getNum(),4,BigDecimal.ROUND_HALF_UP));
                    }
                }
            }
            danenergy=danenergy.divide(new BigDecimal(products.size()),4,BigDecimal.ROUND_HALF_UP);
            danco=danco.divide(new BigDecimal(products.size()),4,BigDecimal.ROUND_HALF_UP);
            carbon.setExhaust(danco);
            carbon.setEnergy(danenergy);
            carbon.setAllexhaust(co);
            carbon.setAllEnergy(energy);
        }else {
            carbon.setExhaust(danco);
            carbon.setEnergy(danenergy);
            carbon.setAllexhaust(co);
            carbon.setAllEnergy(energy);
        }

        return carbon;
    }

    /**
            * 自评
     * 自动计算自评分  总体情况 数据
     * @param check
     * @param score
     * @return*/

    public static  CarbonDeclareScore  computeDeclare(CarbonDeclare check, CarbonDeclareScore score){
        //        计算
//        市场履约率
        if(check.getFiveYearFulfill()!=null&&check.getFiveYearFulfill().equals(1))
            score.setFulfillRate(new BigDecimal(100));
        else
            score.setFulfillRate(new BigDecimal(0));
//        碳资产情况
        if(check.getCcerNum().compareTo(BigDecimal.ZERO)>0
                || check.getVcsNum().compareTo(BigDecimal.ZERO)>0
                || check.getGreenElectricityNum().compareTo(BigDecimal.ZERO)>0)
            score.setPropertyAuto(new BigDecimal(100));
        else  score.setPropertyAuto(new BigDecimal(0));
//    绿色供应链
        if(check.getGreenSupplyLevel()==null)
            score.setGreenISCAuth(new BigDecimal(0));
        else if(check.getGreenSupplyLevel().equals(14))
            score.setGreenISCAuth(new BigDecimal(100));
        else if(check.getGreenSupplyLevel().equals(15))
            score.setGreenISCAuth(new BigDecimal(60));
        else  score.setGreenISCAuth(new BigDecimal(0));
//      绿色供应链平台
        if(check.getGreenTrace()==null||check.getGreenTrace().equals(0))
            score.setGreenISCPlate(new BigDecimal(0));
        else  score.setGreenISCPlate(new BigDecimal(100));
//        绿色工厂认证
        if(check.getGreenFactoryLevel()==null)
            score.setExpandAuth(new BigDecimal(0));
        else if(check.getGreenFactoryLevel().equals(14))
            score.setExpandAuth(new BigDecimal(100));
        else if(check.getGreenFactoryLevel().equals(15))
            score.setExpandAuth(new BigDecimal(60));
        else  score.setExpandAuth(new BigDecimal(0));
//        绿色产品认证
        int num=0;
        if(check.getGreenProduct()!=null)
            num=check.getGreenProduct().size();
        num=num*10;
        score.setExpandNum(new BigDecimal(num));
        if(score.getProductEnergy()==null)
            score.setProductEnergy(new BigDecimal(0));
        if(score.getEnergyStruct()==null)
            score.setEnergyStruct(new BigDecimal(0));
        if(score.getCarbonIntensity()==null)
            score.setCarbonIntensity(new BigDecimal(0));
        if(score.getCarbonStruct()==null)
            score.setCarbonStruct(new BigDecimal(0));
        if(score.getGreenEnergyAve()==null)
            score.setGreenEnergyAve(new BigDecimal(0));
        if(score.getGreenEnergyTwenty()==null)
            score.setGreenEnergyTwenty(new BigDecimal(0));
        if(score.getPropertyScore()==null)
            score.setPropertyScore(new BigDecimal(0));
        return score;
    }
    /**
            * 自评
     * 计算  其他行业 单位排放及总排放
     * @param declare
     * @param carbon
     * @return*/

    public static CompanyCarbon computeCDeclare(CarbonDeclare declare, CompanyCarbon carbon){
        Map<String,BigDecimal> map=CarbonDeclareUtil.computeDeclare(declare.getOtherProduce());
        if(map!=null){
            carbon.setAllEnergy(map.get("energy"));
            carbon.setAllexhaust(map.get("carbon"));
            carbon.setEnergy(map.get("danEnergy"));
            carbon.setExhaust(map.get("danCarbon"));
        }else {
            carbon.setAllEnergy(new BigDecimal(0));
            carbon.setAllexhaust(new BigDecimal(0));
            carbon.setEnergy(new BigDecimal(0));
            carbon.setExhaust(new BigDecimal(0));
        }
        return carbon;
    }

  /**
            * ()计算碳评分
     * @param score
     * @param carbon
     * @return*/

    public static CompanyCarbon computeCarbon(CarbonDeclareScore score,CompanyCarbon carbon){
//       能耗水平
        carbon.setEnergyExpend(score.getProductEnergy().multiply(new BigDecimal(0.8)).
                add(score.getEnergyStruct().multiply(new BigDecimal(0.2)))
                .setScale(2,BigDecimal.ROUND_HALF_UP));
//       碳排放水平
        carbon.setCarbonLet(score.getCarbonIntensity().multiply(new BigDecimal(0.8))
                .add(score.getCarbonStruct().multiply(new BigDecimal(0.2)))
                .setScale(2,BigDecimal.ROUND_HALF_UP));
//       绿碳水平
        carbon.setGreenCarbon(score.getFulfillRate().multiply(new BigDecimal(0.1))
                .add(score.getGreenEnergyAve().multiply(new BigDecimal(0.3)))
                .add(score.getGreenEnergyTwenty().multiply(new BigDecimal(0.2)))
                .add(score.getPropertyAuto().multiply(new BigDecimal(0.25)))
                .add(score.getPropertyScore().multiply(new BigDecimal(0.15)))
                .setScale(2,BigDecimal.ROUND_HALF_UP));
//       绿色供应链水平
        carbon.setSupplyChainGreen(score.getGreenISCAuth().multiply(new BigDecimal(0.5))
                .add(score.getGreenISCPlate().multiply(new BigDecimal(0.5)))
                .setScale(2,BigDecimal.ROUND_HALF_UP));
//       可拓展指标
        carbon.setExpand(score.getExpandAuth().multiply(new BigDecimal(0.5))
                .add(score.getExpandNum().multiply(new BigDecimal(0.5)))
                .setScale(2,BigDecimal.ROUND_HALF_UP));
        carbon.setMark(carbon.getEnergyExpend().multiply(new BigDecimal(0.3))
                .add(carbon.getCarbonLet().multiply(new BigDecimal(0.4)))
                .add(carbon.getGreenCarbon().multiply(new BigDecimal(0.15)))
                .add(carbon.getSupplyChainGreen().multiply(new BigDecimal(0.1)))
                .add(carbon.getExpand().multiply(new BigDecimal(0.05)))
                .setScale(2,BigDecimal.ROUND_HALF_UP)
        );
        return carbon;

    }

}
